import React, {Component} from 'react';
import reactDom from 'react-dom';

import Uploader from '../../../../public/components/UploaderImgWithBase64';
import nodeRefs from './nodeRefs';

import {
    toast
} from 'nc-lightapp-front';

// import language from '../../../language';

const imageList = ['.png', '.jpg', '.gif', '.jpeg'];

export default function() {
    
    if(document.getElementById('photoDiv')) {
        return;
    }

    const {
        form: {
            setFormItemsVisible
        },
        main,
        MultiInit
    } = this.props;
    const { language } = main;
    let { imageData, isDisableUpload } = main;
    setFormItemsVisible('bd_psndoc', {
        photo: false
    });
    let timer = null;
    const check = () => {
        let oFormBox = document.getElementById('form_bd_psndoc').children[0];
        let obox = oFormBox.querySelectorAll("div[class='group-form-item lightapp-component-form show-form']")[0];
        if(obox) {
            let oDiv = document.createElement('div');
            oDiv.id = 'photoDiv';
            oDiv.style.float = 'right';
            oDiv.style.marginRight = '10px';
            oDiv.style.marginTop = '8px';

            obox.insertBefore(oDiv, obox.children[0]);
            reactDom.render(
                <Uploader
        
                    ref={node => nodeRefs.disableUploadRefs = node}
                    
                    onUpload={(result, file) => {
                        let filename = file.name;
                        let fileExt = filename.substring(filename.lastIndexOf('.'));
        
                        if(imageList.includes(fileExt.toLowerCase())) {
                            this.props.dispatch({
                                type: 'main/update',
                                payload: {
                                    imageData: result
                                }
                            })
                        }
                        else {
                            toast({
                                color: 'danger',
                                content: language['hi6007-000177'] /* 上传格式不正确 */
                            });
        
                            return false;
                        }
                    }}
                    value={imageData}
                    MultiInit={MultiInit}
            />, oDiv);
            if (timer) {
                clearTimeout(timer)
            }
        }else {
            timer = setTimeout(check, 100)
        }
    }
    check();
}
